<template>
  <div class="home-content">
    <div class="content-nav fixclear" v-show="fixed">
      <div class="search">
        <i class="menu-icon" @click="handleClickMenu"></i>
        <input type="text" placeholder="请输入关键词">
        <i class="search-icon"></i>
      </div>
    </div>
    <div class="content-nav" :class="fixed ? 'fixed' : ''" ref="searchBox">
      <div class="search">
        <i class="menu-icon"  @click="handleClickMenu"></i>
        <input type="text" placeholder="请输入标题关键词">
        <i class="search-icon"></i>
      </div>
    </div>
    <div class="content-list">
      <div class="list-item" v-for="(item, index) of list" :key="index">
        <div class="item-title">
          <i></i>杜木土
        </div>
        <div class="item-content">
          <p>{{item.title}}</p>
          <p>{{item.desc}}</p>
        </div>
        <div class="item-operation">
          <div class="left">
            <div class="time"><i></i>{{item.time}}</div>
            <div class="comment"><i></i>{{item.comments}}</div>
          </div>
          <div class="right tag"><i></i>{{item.tag}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HomeContent',
  data () {
    return {
      fixed: false,
      list: [
        {
          title: '这是一条标题',
          desc: '这是我写的一条文章梗概，可能就是文章的前多少字吧，现在没有写后台，现在还不太确定以后会怎样规划...',
          time: '2020-09-02',
          comments: 2,
          tag: '生活趣事'
        },
        {
          title: '这是一条标题',
          desc: '这是我写的一条文章梗概，可能就是文章的前多少字吧，现在没有写后台，现在还不太确定以后会怎样规划...',
          time: '2020-09-02',
          comments: 2,
          tag: '生活趣事'
        },
        {
          title: '这是一条标题',
          desc: '这是我写的一条文章梗概，可能就是文章的前多少字吧，现在没有写后台，现在还不太确定以后会怎样规划...',
          time: '2020-09-02',
          comments: 2,
          tag: '生活趣事'
        },
        {
          title: '这是一条标题',
          desc: '这是我写的一条文章梗概，可能就是文章的前多少字吧，现在没有写后台，现在还不太确定以后会怎样规划...',
          time: '2020-09-02',
          comments: 2,
          tag: '生活趣事'
        }
      ]
    }
  },
  methods: {
    handleScroll () {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      const benchmark = parseFloat(document.documentElement.style.cssText.split(':')[1].split('px')[0].trim())
      const el = this.$refs.searchBox
      if (scrollTop >= benchmark * 1.85) {
        this.fixed = true
        el.style.position = 'fixed'
        el.style.top = '0'
        el.style.left = '0'
      } else {
        this.fixed = false
        el.style.position = ''
        el.style.top = ''
        el.style.left = ''
      }
    },
    handleClickMenu () {
      this.$emit('handleEmitMenu')
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  destroyed () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="scss" scoped>
.home-content {
  width: 100%;
  .content-nav {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 0 4%;
    box-sizing: border-box;
    background: transparent;
    box-shadow: 0px 10px 15px -10px rgba(221, 221, 221, 0);
    transition: all 0.1s ease;
    .search {
      width: 100%;
      height: 0.42rem;
      padding: 0.05rem 0;
      margin: 0.15rem 0;
      position: relative;
      input {
        border: none;
        background: rgba($color: #ffffff, $alpha: 1.0);
        width: 100%;
        height: 100%;
        border-radius: 0.2rem;
        padding: 0 0.58rem 0 0.15rem;
        box-sizing: border-box;
        box-shadow: 0px 10px 15px -10px rgba(221, 221, 221, 1);
        transition: all 0.1s ease;
      }
      .menu-icon {
        width: 0.3rem;
        height: 0.3rem;
        position: absolute;
        top: 0.11rem;
        right: 0;
        background: url('../../../assets/images/menu.svg') 50% 50% no-repeat;
        background-size: 100%;
        opacity: 0;
        transition: all 0.1s ease;
      }
      .search-icon {
        width: 0.42rem;
        height: 0.42rem;
        position: absolute;
        top: 0.05rem;
        right: 0.1rem;
        background: url('../../../assets/images/search-icon.svg') 50% 50% no-repeat;
        background-size: 80%;
        transition: all 0.1s ease;
      }
    }
    &.fixed {
      background: #ffffff;
      box-shadow: 0px 10px 15px -10px rgba(221, 221, 221, 1);
      .search {
        input {
          box-shadow: 0px 10px 15px -10px rgba(221, 221, 221, 0);
          background: rgba($color: #f5f5f5, $alpha: 1.0);
          margin-right: 0.5rem;
          width: calc(100% - 0.5rem);
        }
        .menu-icon {
          opacity: 1;
        }
        .search-icon {
          right: 0.6rem;
        }
      }
    }
    &.fixclear {
      opacity: 0;
    }
  }
  .content-list {
    padding: 0 4%;
    box-sizing: border-box;
    .list-item {
      margin-bottom: 0.2rem;
      &:last-child {
        margin-bottom: 0;
      }
      background: #ffffff url('../../../assets/images/topic.svg') 100% 0% no-repeat;
      border-radius: 0.2rem;
      padding: 0.2rem;
      box-shadow: 0px 10px 15px -10px #ddd;
      .item-title {
        display: flex;
        align-items: center;
        margin-bottom: 0.1rem;
        i {
          width: 0.3rem;
          height: 0.3rem;
          background: url("../../../assets/images/user-icon.svg") 50% 50% no-repeat;
          background-size: 100%;
          border-radius: 50%;
          margin-right: 0.05rem;
        }
      }
      .item-content {
        p {
          margin-bottom: 0.1rem;
          &:first-child {
            font-size: 0.17rem;
            font-weight: bold;
          }
          &:last-child {
            line-height: 1.4;
          }
        }
      }
      .item-operation {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .left {
          display: flex;
          align-items: center;
          .time {
            display: flex;
            align-items: center;
            font-size: 0.12rem;
            color: #a5a5a5;
            margin-right: .15rem;
            i {
              width: .2rem;
              height: .2rem;
              background: url('../../../assets/images/time.svg') 50% 50% no-repeat;
              background-size: 100%;
              margin-right: 0.02rem;
            }
          }
          .comment {
            display: flex;
            align-items: center;
            font-size: 0.12rem;
            color: #a5a5a5;
            margin-right: .1rem;
            i {
              width: .2rem;
              height: .2rem;
              background: url('../../../assets/images/comment.svg') 50% 50% no-repeat;
              background-size: 100%;
              margin-right: 0.02rem;
            }
          }
        }
        .right {
          &.tag {
            display: flex;
            align-items: center;
            font-size: 0.12rem;
            color: #a5a5a5;
            margin-right: .1rem;
            i {
              width: .2rem;
              height: .2rem;
              background: url('../../../assets/images/tag.svg') 50% 50% no-repeat;
              background-size: 100%;
              margin-right: 0.02rem;
            }
          }
        }
      }
    }
  }
}
</style>
